using System;
using System.Collections.Generic;
using System.Text;

namespace BizTalk.ESB.Extensions.Helpers
{
    public class DebugMessageFactory
    {
        public static String GenerateTraceMessage(String Source, String SourceInstanceId, String Message, System.Diagnostics.EventLogEntryType DebugEventType)
        {
            StringBuilder DebugMessage = new StringBuilder();
            DebugMessage.Append(System.DateTime.UtcNow.ToString());
            DebugMessage.Append(" ~ ");
            DebugMessage.Append(Source);
            DebugMessage.Append(" :: ");
            DebugMessage.Append(SourceInstanceId);
            DebugMessage.Append(" ~ ");
            DebugMessage.Append(DebugEventType.ToString());
            DebugMessage.Append(" ~ ");
            DebugMessage.Append(Message);
            return DebugMessage.ToString();
        }

        public static void DebugMessageItineraryRoutingProperties(Microsoft.XLANGs.BaseTypes.XLANGMessage message, Boolean DebugFlag, String ServiceName, String ServiceInstanceId)
        {
            Object propServiceName = message.GetPropertyValue(typeof(Microsoft.Practices.ESB.Itinerary.Schemas.ServiceName));
            Object propServiceState = message.GetPropertyValue(typeof(Microsoft.Practices.ESB.Itinerary.Schemas.ServiceState));
            Object propServiceType = message.GetPropertyValue(typeof(Microsoft.Practices.ESB.Itinerary.Schemas.ServiceType));
            
            System.Diagnostics.Trace.WriteLineIf(DebugFlag
                , DebugMessageFactory.GenerateTraceMessage(
                    ServiceName
                    , ServiceInstanceId
                    , "Service Name Property of message " + message.Name + " = " + System.Convert.ToString(propServiceName)
                    , System.Diagnostics.EventLogEntryType.Information));
            System.Diagnostics.Trace.WriteLineIf(DebugFlag
                , DebugMessageFactory.GenerateTraceMessage(
                    ServiceName
                    , ServiceInstanceId
                    , "Service State Property of message " + message.Name + " = " + System.Convert.ToString(propServiceState)
                    , System.Diagnostics.EventLogEntryType.Information));
            System.Diagnostics.Trace.WriteLineIf(DebugFlag
                , DebugMessageFactory.GenerateTraceMessage(
                    ServiceName
                    , ServiceInstanceId
                    , "Service Type Property of message " + message.Name + " = " + System.Convert.ToString(propServiceType)
                    , System.Diagnostics.EventLogEntryType.Information));

            if (message.GetPropertyValue(typeof(WCF.Action)) != null)
            {
                Object propWcfAction = message.GetPropertyValue(typeof(WCF.Action));
                System.Diagnostics.Trace.WriteLineIf(DebugFlag
               , DebugMessageFactory.GenerateTraceMessage(
                   ServiceName
                   , ServiceInstanceId
                   , "WCF Action Property of message " + message.Name + " = " + System.Convert.ToString(propWcfAction)
                   , System.Diagnostics.EventLogEntryType.Information));
            }
        }
    }
}
